package controller.business.fundEntry;

import StaticFinal.CrudType;
import controller.common.BaseController;
import controller.common.StaticConstant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.business.BusinessInfoServices;
import services.business.InspectorFundServices;
import services.business.InspectorInfoServices;
import services.dictionary.FundSetServices;
import services.dictionary.MajorTypeServices;
import services.expertInfo.ExpertMajorServices;
import services.expertInfo.ExpertReviewServices;
import wt.entity.business.BusinessInfo;
import wt.entity.business.BusinessInfoExample;
import wt.entity.business.InspectorInfo;
import wt.entity.dictionary.MajorType;
import wt.entity.expertInfo.ExpertReview;
import wt.staticData.ShortData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller
@RequestMapping("business/fundEntry")
public class FundEntryController extends BaseController {


    @Autowired
    BusinessInfoServices businessInfoServices;
    @Autowired
    InspectorInfoServices inspectorInfoServices;
    @Autowired
    FundSetServices fundSetServices;
    @Autowired
    MajorTypeServices majorTypeServices;

    @Autowired
    InspectorFundServices inspectorFundServices;

    @Autowired
    ExpertMajorServices expertMajorServices;

    @Autowired
    ExpertReviewServices expertReviewServices;




    @RequiresPermissions("business:fundEntry:default")
    @RequestMapping("index")
    public String index() {
        return "business/fundEntry/index";
    }


    @RequiresPermissions("business:fundEntry:default")
    @RequestMapping("editDlg")
    public String editDlg(String businessId, Map map) throws Exception {


        map.put("fundSetList", fundSetServices.selectByExample(null));
        map.put("inspectorInfoList", inspectorInfoServices.selectByBusinessId(businessId));
        map.put("businessId", businessId);
        map.put(StaticConstant.EDIT_URL, "business/fundEntry/insert");
        map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_INSERT);

        return "business/fundEntry/editDlg";
    }

    @RequiresPermissions("business:fundEntry:default")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult insert(InspectorFundList inspectorFundList, String businessId) throws Exception {

        CommonResult commonResult = CommonResult.newCommonResult();

        BusinessInfo businessInfo = businessInfoServices.selectByPrimaryKey(businessId);

        if (0 != businessInfo.getFundStatus()) {
            commonResult.addMessage("已录入");
            return commonResult.setError();
        }

        if (inspectorFundList.getInspectorFundList().size() != businessInfo.getInspectorCount().intValue()) {
            commonResult.addMessage("专家与业务分配专家不匹配");
            return commonResult.setError();
        }





        List<InspectorInfo> inspectorInfoList = inspectorInfoServices.selectByBusinessId(businessInfo.getId());
        List<ExpertReview> expertReviewList = new ArrayList<ExpertReview>(inspectorInfoList.size());
        //专业
        List<MajorType> majorTypeList = majorTypeServices.selectBusinessMajorByLevel(businessInfo.getId(), 2);
        StringBuilder str = new StringBuilder();
        for (MajorType majorType : majorTypeList) {
            str.append("；");
            str.append(majorType.getName());
        }
        //专业过长时，截断。
        int majorFinalLength=str.length()>340?340:str.length();
        String majorStr = majorFinalLength > 0 ? str.substring(1, majorFinalLength) :"无";

        for (InspectorInfo inspectorInfo : inspectorInfoList) {

            ExpertReview expertReview = new ExpertReview();
            expertReview.setExpertId(inspectorInfo.getExpertId());
            expertReview.setRole("leader".equals(inspectorInfo.getExpertRole()) ? ShortData.s1 : ShortData.s0);
            expertReview.setCompany(businessInfo.getCompanyName());
            expertReview.setProduct(majorStr);
            expertReview.setStartTime(businessInfo.getStartTime());
            expertReview.setEndTime(businessInfo.getEndTime());
            expertReview.setCrudType(CrudType.select);
            expertReview.setExtend1(businessInfo.getItemNum());
            expertReviewList.add(expertReview);

        }

        expertReviewServices.adminInsertSelective(expertReviewList);

        inspectorFundServices.insertSelective(inspectorFundList.getInspectorFundList(), businessId, getLoginAccount().getStaffInfo().getId(), getLoginAccount().getStaffInfo().getName());


        return commonResult.setSuccess();
    }


    @RequiresPermissions("business:fundEntry:default")
    @ResponseBody
    @RequestMapping("select")
    public List<BusinessInfo> select() throws Exception {
        BusinessInfoExample businessInfoExample = new BusinessInfoExample();
        businessInfoExample.createCriteria().andManagerIdEqualTo(getLoginAccount().getStaffInfo().getId()).andBusinessWorkFlowEqualTo(ShortData.s3).andFundStatusEqualTo(ShortData.s0);
        return businessInfoServices.selectByExample(businessInfoExample);
    }

    @RequiresPermissions("business:fundEntry:default")
    @ResponseBody
    @RequestMapping("welcomeTips")
    public CommonResult welcomeTips() throws Exception {
        CommonResult commonResult = new CommonResult();
        int count = select().size();
        if (count == 0) {
            return commonResult.setError();
        } else {
            Map map = new HashMap();
            map.put("info", count + "条业务经费未录入");
            map.put("url", "business/fundEntry/index");
            commonResult.setData(map);
            return commonResult.setSuccess();
        }
    }


}
